package io.hx.modules.hos.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.hx.common.validator.ValidatorUtils;
import io.hx.modules.patient.entity.FilesEntity;
import io.hx.modules.patient.entity.PatientUserEntity;
import io.hx.modules.patient.service.FilesService;
import io.hx.modules.patient.service.PatientUserService;
import io.hx.modules.rep.entity.DiagnosticEntity;
import io.hx.modules.rep.service.DiagnosticService;
import io.hx.modules.sys.entity.SysDeptEntity;
import io.hx.modules.sys.entity.SysUserEntity;
import io.hx.modules.sys.service.SysDeptService;
import io.hx.modules.vo.DiagnoseVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import io.hx.modules.hos.entity.AppointmentEntity;
import io.hx.modules.hos.service.AppointmentService;
import io.hx.common.utils.PageUtils;
import io.hx.common.utils.R;

import javax.servlet.http.HttpSession;


/**
 * 预约表
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2023-03-21 11:36:40
 */
@Controller
@RequestMapping("hos/appointment")
public class AppointmentController {
    @Autowired
    private AppointmentService appointmentService;
    @Autowired
    private PatientUserService patientUserService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private DiagnosticService diagnosticService;
    @Autowired
    private FilesService filesService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @ResponseBody
    @RequiresPermissions("hos:appointment:list")
    public R list(@RequestParam Map<String, Object> params) {

        PageUtils page = appointmentService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    @RequiresPermissions("hos:appointment:info")
    public R info(@PathVariable("id") Integer id) {
        AppointmentEntity appointment = appointmentService.getById(id);
        //获取患者信息
        Integer userId = appointment.getUserId();
        PatientUserEntity patientUser = patientUserService.queryById(userId);
        appointment.setUser(patientUser);
        //获取部门
        Long outpatientsId = (long) appointment.getOutpatientsId();
        SysDeptEntity deptEntity = sysDeptService.queryByDeptId(outpatientsId);
        appointment.setSysDeptEntity(deptEntity);
        System.out.println("信息:" + appointment.toString());
        return R.ok().put("appointment", appointment);
    }

    /**
     * 保存(诊断)
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @RequiresPermissions("hos:appointment:save")

    public String save(DiagnoseVo diagnoseVo, HttpSession session) {
        SysUserEntity sysUser = (SysUserEntity) session.getAttribute("user");
        //预约表
        AppointmentEntity appointment = new AppointmentEntity();
        //诊断报告
        DiagnosticEntity diagnosticEntity = new DiagnosticEntity();
        //档案
        FilesEntity filesEntity = new FilesEntity();
        //上传时间
        diagnosticEntity.setUploadTime(new Date());
        //检查部位
        diagnosticEntity.setReportName(diagnoseVo.getReportName());
        //患者id
        diagnosticEntity.setUserId(diagnoseVo.getPatientId());
        //医生id
        diagnosticEntity.setSysUserId(sysUser.getUserId().intValue());
        //状态
        diagnosticEntity.setStatus(1);
        //描述类容
        diagnosticEntity.setContent("病情描述:"+diagnoseVo.getRemak()+"\n" +"诊断结果:"+diagnoseVo.getDiagnose()+"\n" +"病史:"+diagnoseVo.getMedical());
        //检测部位
        diagnosticEntity.setMoney(diagnoseVo.getPrice());
        //预约id
        diagnosticEntity.setAppointmentId(diagnoseVo.getAppointmentId());
        boolean save = diagnosticService.save(diagnosticEntity);
        appointment.setId(diagnoseVo.getAppointmentId());
        appointment.setAppointmentStatus(3);
        boolean b = appointmentService.updateById(appointment);
        return "/modules/hos/appointment" ;
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    @RequiresPermissions("hos:appointment:update")
    public R update(@RequestBody AppointmentEntity appointment){
        ValidatorUtils.validateEntity(appointment);
        appointmentService.updateById(appointment);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions("hos:appointment:delete")
    public R delete(@RequestBody Integer[] ids){
        appointmentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
